﻿Imports VendasDAL
Imports EstoqueBLL
Public Class AgendamentoBLL

    Dim ObjDAL As AgendamentoDAL
    Property Cliente As String
    Property Codigo As Integer
    Property Data As Date
    Property HrInicial As DateTime
    Property Obs As String
    Property CodStatus As Integer
    Property DesStatus As String
    Property CodPlaca As Integer
    Property DesPlaca As String
    Property lVendas As New List(Of ItensVendaBLL)

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, data As Date, hrinicial As DateTime, obs As String, codstatus As Integer, codplaca As Integer)
        Me.Codigo = codigo
        Me.Data = data
        Me.HrInicial = hrinicial
        Me.Obs = obs
        Me.CodStatus = codstatus
        Me.CodPlaca = codplaca
    End Sub

#End Region


#Region "Métodos"

#Region "Carrega"
    Private Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New AgendamentoDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = tab.Rows(0).Item("age_codigo").ToString
                Me.Data = tab.Rows(0).Item("age_data").ToString
                Me.HrInicial = tab.Rows(0).Item("age_horainicio").ToString()
                Me.Obs = tab.Rows(0).Item("age_obs").ToString
                Me.CodStatus = tab.Rows(0).Item("sta_codigo").ToString
                Me.CodPlaca = tab.Rows(0).Item("pla_codigo").ToString
                Me.DesStatus = tab.Rows(0).Item("sta_descricao").ToString
                Me.DesPlaca = tab.Rows(0).Item("pla_descricao").ToString
                Me.Cliente = tab.Rows(0).Item("cli_nome").ToString
                carregaTodosItemVenda()
            Else
                limpa()
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Carrega itens venda"
    Sub carregaTodosItemVenda()
        Dim obj As ItensVendaBLL
        Dim tab As DataTable
        Try
            obj = New ItensVendaBLL
            obj.IteCodigo = Me.Codigo
            tab = obj.LocalizarServicos()
            For Each linha As DataRow In tab.Rows
                obj = New ItensVendaBLL
                obj.IteCodigo = linha("age_codigo").ToString
                obj.ObjServico = New ServicoBLL(linha("pro_codigo").ToString())
                lVendas.Add(obj)
            Next
        Catch ex As Exception
            Throw ex
        Finally
            obj = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa"
    Sub limpa()
        Me.Codigo = Nothing
        Me.Data = Nothing
        Me.HrInicial = Nothing
        Me.Obs = Nothing
        Me.CodPlaca = Nothing
        Me.CodStatus = Nothing
        Me.DesStatus = Nothing
        lVendas.Clear()
    End Sub
#End Region

#Region "Excluir itens venda"

    Sub ExcluirItensVenda()
        Dim Obj As ItensVendaBLL
        Try
            Obj = New ItensVendaBLL
            Obj.IteCodigo = Me.Codigo
            Obj.ExcluirServicos()
        Catch ex As Exception
            Throw ex
        Finally
            Obj = Nothing
        End Try
    End Sub

#End Region

#Region "Excluir"
    Function Excluir() As Boolean
        If (Me.Codigo <> 0) Then
            Dim ret As Boolean = False
            Dim retlinhas As Integer
            Try
                ObjDAL = New AgendamentoDAL
                retlinhas = ObjDAL.Excluir(Me.Codigo)
                If (retlinhas > 0) Then
                    ret = True
                End If
            Catch ex As Exception
                Throw ex
                ret = False
            Finally
                ObjDAL = Nothing
            End Try
            Return ret
        Else
            Return False
        End If
    End Function
#End Region

#Region "Inserir itens venda"
    Sub InserirItensVenda()
        Try
            For Each item As ItensVendaBLL In lVendas
                item.IteCodigo = Me.Codigo
                item.InserirServicos()
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

#Region "Inserir"
    Function Inserir() As Boolean
        Dim ret As Boolean
        Try

            If (Valida()) Then
                ObjDAL = New AgendamentoDAL
                Me.Codigo = ObjDAL.Inserir(Me.Codigo, Me.Data, Me.HrInicial, Me.Obs, Me.CodPlaca, Me.CodStatus)
                InserirItensVenda()
                ret = True
                Return True
            End If
        Catch ex As Exception
            Throw ex
            Ret = False
            Return False
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Valida Campos"
    Public Function Valida() As Boolean
        If CodPlaca = Nothing Or CodPlaca = 0 Then Throw New Exception("Preencha campo placa.")
        If FormatDateTime(Data) < FormatDateTime(Date.Now, vbShortDate) Then Throw New Exception("Data do agendamento menor que data atual.")
        If FormatDateTime(Date.Now(), vbShortDate) = FormatDateTime(Data, vbShortDate) Then
            If FormatDateTime(Date.Now(), vbShortTime) > FormatDateTime(HrInicial, vbShortTime) Then Throw New Exception("Horário do agendamento menor que hora atual")
        End If
        Return True
    End Function
#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New AgendamentoDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function

    Function locdata(ByVal dataIni As DateTime, ByRef datafim As DateTime) As DataTable 'buscar datas cbfiltro
        Dim ret As DataTable
        Try
            ObjDAL = New AgendamentoDAL
            If dataIni <= datafim Then
                ret = ObjDAL.locadata(dataIni, datafim)
            Else : Throw New Exception("Data inical maior que data final")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Alterar"
    Function Alterar() As Boolean
        Dim ret As Boolean
        Try
            If (Valida()) Then
                ObjDAL = New AgendamentoDAL
                ObjDAL.Alterar(Me.Codigo, Me.Data, Me.HrInicial, Me.Obs, Me.CodPlaca, Me.CodStatus)
                If (Me.Codigo > 0) Then
                    ExcluirItensVenda()
                    InserirItensVenda()
                    ret = True
                    Return True
                End If
            End If
        Catch ex As Exception
            Throw ex
            ret = False
            Return False
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#End Region
End Class
